package com.abc.web0415;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;

import java.io.IOException;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        ServletContext servletContext = this.getServletContext();//jsp中的application
        System.out.println(servletContext.getAttribute("count"));


        String username = request.getParameter("username");
        String pwd = request.getParameter("pwd");

        if("zs".equals(username) && "123".equals(pwd)){
            Cookie cookie = new Cookie("un", username);
            cookie.setMaxAge(60*60*24);
            //cookie.setDomain("/");
            response.addCookie(cookie);


            HttpSession session = request.getSession(true);//写true跟无参效果一样
            session.setAttribute("un", username);
            System.out.println(session.getId());
//session.removeAttribute("un");
            session.setMaxInactiveInterval(60*30);

            response.sendRedirect("center.jsp");
        }else{

            response.sendRedirect("index.jsp");
        }
    }
}
